import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: '我的应用',
    home: MyScaffold(),
  ));
}

class MyAppBar extends StatelessWidget {
  // const MyAppBar({Key key}) : super(key: key);
  MyAppBar({this.title});

  final Widget title;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 56,
      padding: const EdgeInsets.symmetric(horizontal: 8),
      decoration: BoxDecoration(color: Colors.blue[500]),
      child: Row(
        children: <Widget>[
          IconButton(
            icon: Icon(Icons.menu),
            tooltip: '导航菜单',
            onPressed: null,
          ),
          Expanded(
            child: title,
          ),
          IconButton(
            icon: Icon(Icons.search),
            tooltip: '搜索',
            onPressed: null,
          )
        ],
      ),
    );
  }
}

class MyScaffold extends StatelessWidget {
  // const MyScaffold({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Column(
        children: <Widget>[
          MyAppBar(
            title: Text(
              '示例标题',
              style: Theme.of(context).primaryTextTheme.headline6,
            ),
          ),
          Expanded(
            child: Center(
              //child: Text('🉑'),
              // child: MyButton(),
              child: Counter(),
            ),
          )
        ],
      ),
    );
  }
}

// 一个可点击的按钮
class MyButton extends StatelessWidget {
  // const MyButton({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        print('按钮被点击了');
      },
      child: Container(
        height: 36,
        padding: EdgeInsets.all(8),
        margin: EdgeInsets.symmetric(horizontal: 8.0),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5),
          color: Colors.lightGreen[500],
        ),
        child: Center(
          child: Text('Engage'),
        ),
      ),
    );
  }
}

// 根据用户输入改变 widget
class Counter extends StatefulWidget {
  // Counter({Key key}) : super(key: key);

  @override
  _CounterState createState() => _CounterState();
}

class _CounterState extends State<Counter> {
  int _counter = 0;

  void _increment() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // return Row(
    //   children: <Widget>[
    //     RaisedButton(
    //       onPressed: _increment,
    //       child: Text('Increment'),
    //     ),
    //     Text('Count: $_counter'),
    //   ],
    // );

    // 使用新控件
    return Row(
      children: <Widget>[
        CounterIncrementor(
          onPressed: _increment,
        ),
        CounterDisplay(
          count: _counter,
        )
      ],
    );
  }
}

class CounterDisplay extends StatelessWidget {
  // const CounterDisplay({Key key}) : super(key: key);
  CounterDisplay({this.count});
  final int count;

  @override
  Widget build(BuildContext context) {
    return Text('次数：$count');
  }
}

class CounterIncrementor extends StatelessWidget {
  // const CounterIncrementor({Key key}) : super(key: key);
  CounterIncrementor({this.onPressed});
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: onPressed,
      child: Text('增'),
    );
  }
}
